package com.yanqu.road.dao.impl.activity.stronghold;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.dao.impl.logic.ResultSetParseEntityManger;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossStrongholdRankDaoImpl extends TempDao {

    public Map<Integer, Map<Integer, LinkedList<CrossUserRank>>> getCrossStrongholdRankListMap(List<Integer> activityIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, LinkedList<CrossUserRank>>> dataMap = new ConcurrentHashMap<>();
        if(null == activityIdList || activityIdList.size() <= 0){
            return dataMap;
        }
        String sql = "select * from t_c_activity_stronghold_rank where activityId in(" + StringUtils.listToString(activityIdList, ",") + ") ";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUserRank data = initCrossOreWarRank(rs);
                if(!dataMap.containsKey(data.getType())){
                    dataMap.put(data.getType(), new ConcurrentHashMap<>());
                }
                Map<Integer, LinkedList<CrossUserRank>> typeMap = dataMap.get(data.getType());
                LinkedList<CrossUserRank> linkedList = typeMap.computeIfAbsent(data.getBigRankType(), k -> new LinkedList<>());
                linkedList.add(data);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public boolean updateCrossStrongholdRankList(List<CrossUserRank> rankList) {
        String sql = "REPLACE INTO t_c_activity_stronghold_rank(activityId,conditionType,rank,userId,userBaseInfo,lastUpdateTime,value) VALUES(?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            int rank = 1;
            for (CrossUserRank userRank : rankList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER, userRank.getType()));
                para.put(i++, new DbParameter(Types.INTEGER, rank));
                para.put(i++, new DbParameter(Types.BIGINT, userRank.getUserId()));
                para.put(i++, new DbParameter(Types.NVARCHAR, userRank.getUserBaseInfoJsonStr()));
                para.put(i++, new DbParameter(Types.BIGINT, userRank.getLastUpdateTime()));
                para.put(i++, new DbParameter(Types.VARCHAR, userRank.getValue().toString()));
                mapList.add(para);
                rank++;
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    private CrossUserRank initCrossOreWarRank(ResultSet rs) throws SQLException {
        return ResultSetParseEntityManger.initCrossUserRank(rs);
    }
}
